package com.sjk.flink.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * @author SAMA
 */
public class ObjectMapperUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        // 如果json中有新增的字段并且是实体类类中不存在的，不报错
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        // 如果存在未知属性，则忽略不报错
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 允许key没有双引号
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        // 允许key有单引号
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        // 允许整数以0开头
        objectMapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        // 允许字符串中存在回车换行控制符
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        // 解析器默认不能识别 “Not-a-Number" (NaN)，不会认为其为浮点类型或者int类型的数字：
        objectMapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
        //当遇到未知属性时防止出现异常:
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    public static ObjectMapper getObjectMapper(){
        return objectMapper;
    }

    public static void main(String[] args) throws Exception{
        String scrData ="{}";
        ObjectNode readTree = (ObjectNode)ObjectMapperUtil.getObjectMapper().readTree(scrData);
        readTree.put("JCZ",0.24719999999999998);
        System.out.println(ObjectMapperUtil.getObjectMapper().writeValueAsString(readTree));
    }

}
